package com.booksystem.campuslibrarymanagementsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.booksystem.campuslibrarymanagementsystem.commen.request.userquest.BookBorrowrequst.Approval;
import com.booksystem.campuslibrarymanagementsystem.commen.result.userresuilt.ApprovalList;
import com.booksystem.campuslibrarymanagementsystem.entity.BooksTable;
import com.booksystem.campuslibrarymanagementsystem.entity.BorrowApprovalsTable;
import com.booksystem.campuslibrarymanagementsystem.entity.BorrowRecordsTable;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface BorrowApprovalsTableMapper extends BaseMapper<BorrowApprovalsTable> {
    //通过图书id查询图书表，获得借出数量和总数
    @Select("SELECT * FROM books_table\n" +
            "WHERE bookid =  #{bookid} ")
    Approval queryid(int bookid);

    //判断是否重复借阅
    @Select("SELECT * \n" +
            "FROM borrowapprovals_table \n" +
            "WHERE account = #{account} AND bookid = #{bookid}")
    List<BorrowApprovalsTable> existsByAccountAndBookidi(String account, int bookid);


    //通过借阅成功，更新图书表的借出数量
    @Update("UPDATE books_table\n" +
            "SET availablecopies = availablecopies + 1\n" +
            "WHERE bookid = #{bookid}")
    int updatebook(Approval book);

    //进行借阅
    @Insert("INSERT INTO borrowapprovals_table (bookid, borrowdate, expirationdate , approvalstate,account) VALUES (#{bookid}, #{borrowdate}, #{expirationdate}, #{approvalstate},#{account})")
    boolean save(BorrowApprovalsTable hh);

    //通过状态是否通过查看借阅列表
    @Select("select * from borrowapprovals_table where account =#{account} and approvalstate = #{approvalstate}")
    List<ApprovalList> returnliststatus(String account, String approvalstate);

    //该账户查询所有借阅列表
    @Select("select recordid,bookid, borrowdate, expirationdate , approvalstate,account from borrowapprovals_table where account = #{account}")
    List<ApprovalList> selectByaccount(@Param("account") String account);

    //删除该账户的借阅记录
    @Delete("DELETE FROM borrowapprovals_table WHERE account = #{account} AND recordid = #{recordid}")
    int deleteByAccountAndBookid(@Param("account") String account, @Param("recordid") int recordid);

    //判断预约图书是否存在
    @Select("SELECT * \n" +
            "FROM books_table \n" +
            "WHERE  bookid = #{bookid}")
    BooksTable aaa(int bookid);

    //通过账户id,图书id查询借阅表，查看借阅时间
    @Select("SELECT expirationdate FROM borrowapprovals_table\n" +
            "WHERE bookid =  #{bookid} and account = #{account} ")
    String querytime(int bookid , String account);

    //通过账户id查询借阅记录，获得该账户借阅记录
    @Select("SELECT * FROM borrowapprovals_table\n" +
            "WHERE account =  #{account} ")
    List<BorrowApprovalsTable> queryByAccount(String account);

    //通过借阅审批成功，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE borrowapprovals_table SET approvaldate = #{approvalTime}, approvalstate = '不通过' WHERE account = #{account} And bookid = #{bookid}")
    void unapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);

    //通过借阅审批失败，更新借阅审批表的该审批审批的状态，时间和日期
    @Update("UPDATE borrowapprovals_table SET approvaldate = #{approvalTime}, approvalstate = '通过' WHERE account = #{account} And bookid = #{bookid}")
    void onapproval(@Param("account") String account, @Param("approvalTime") LocalDate approvalTime , @Param("bookid") String bookid,@Param("account") String Approvalstate);

    //    查询账户人员信息
    @Select("select * from borrowapprovals_table where approvalstate = #{state} ")
    List<BorrowApprovalsTable> querydislist(String approvalstate);

}
